# -*- coding: utf-8 -*-
#*******************************************************************************
#   
#   http://www.52kkm.org/\w+/ 解析器                                          
#                                                                               
#*******************************************************************************
from base import *

tag='52kkm'

#---------------------------- 验证模块 ----------------------------

def url_judgement(url):
    if re.match(r'http://www.52kkm.org/\w+/',url):
        return valid_check(url)
    return False

def valid_check(url):
    print('{} catch {}'.format(tag,url))
    content=request_for(url,common_headers);
    if content:
        return get_loader(url)
    return False

def get_loader(url):
    return KkmLoader(url,KkmPageParser,KkmVolParser)

#---------------------------- 下载器 ----------------------------

class KkmLoader(ComicLoader):
    def __init__(self,url,comic_page_parser,comic_vol_parser):
        super().__init__(url,comic_page_parser,comic_vol_parser)
        self.page_encoding='gb2312'
        self.vol_encoding='gb2312'

#---------------------------- 解析器 ----------------------------

class KkmPageParser(ComicPageParser):
    def __init__(self,comic):
        super().__init__(comic)
        self.site_prefix='http://www.52kkm.org'
    def work(self,content):
        # title
        self.comic.title=re.compile(r'<h1>(.*?)</h1>').findall(content)[0]
        # description
        self.comic.description=re.compile(r'<div class="tcont" id="tc1">(.*?)</div>',re.S|re.M).findall(content)[0].strip()
        # cover
        covers_content=re.compile(r'<div class="listslide">(.*?)</div>',re.S).findall(content)[0]
        covers=re.compile(r'src="(.*?)"').findall(covers_content)
        self.comic.cover=covers[0]
        # vols
        vols_content=re.compile(r'<ul class="piclist listcon">(.*?)</ul>',re.S|re.M).findall(content)[0]
        self.feed(vols_content)
        self.comic.vols.reverse()
    def handle_starttag(self,tag,attrs):
        if tag=='a':
            infos=info_collect(attrs)
            self.comic.vols.append(InfoVol(self.site_prefix+infos['href'],infos['title']))

class KkmVolParser(ComicVolParser):
    def __init__(self):
        super().__init__()
        self.page_urls=[]
    def work(self,content):
        self.get_cur_image_url(content)
        comic_name=re.compile(r'<span><a href="(.*?)" class="fh">',re.S).findall(content)[0]
        self.page_prefix='http://www.52kkm.org'+comic_name
        page_urls_content=re.compile(r'<ul class="pagelist"(.*?)</ul>',re.S|re.M).findall(content)[0]
        #print(page_urls_content)
        self.feed(page_urls_content)
        self.page_urls=self.page_urls[2:-1]
        self.pic_index=len(self.page_urls)+1
        for url in self.page_urls:
            content=request_for(url,common_headers).decode('gb2312','ignore')
            self.get_cur_image_url(content)
            self.pic_index-=1
    def handle_starttag(self,tag,attrs):
        if tag=='a' and len(attrs)>0:
            infos=info_collect(attrs)
            self.page_urls.append(self.page_prefix+infos['href'])
            
    def get_cur_image_url(self,content):
        pic_url=re.compile(r'<li class="left" id="imgshow"(.*?)</li>',re.S).findall(content)[0]
        pic_url=re.compile(r'src="(.*?)"').findall(pic_url)[0]
        self.pic_urls.append((pic_url,zero_prefix(self.pic_index)))
